home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / MATH / NS_ROOT / NS_DEMO.PAS next >
Pascal/Delphi Source File  |  1992-05-11  |  7KB  |  212 lines

  1. {$R NS_Demo.Res}
  2.  
  3. Program NS_Demo;
  4.  
  5. Uses
  6.     WinTypes,
  7.    WinCRT,
  8.    WinProcs,
  9.    WObjects,
  10.    Strings,
  11.    NS_Roots;
  12.  
  13. Const
  14.       TheMenu     = 'MainMenu';
  15.  
  16.    id_Run        = 199;
  17.       id_Brent    = 101;
  18.       id_Bisection= 102;
  19.       id_Newton   = 103;
  20.       id_Solve    = 107;
  21.       id_X1       = 109;
  22.       id_X2       = 110;
  23.       id_Tolerance= 111;
  24.       id_IterMax  = 112;
  25.    id_Iter     = 201;
  26.       id_RootValue= 113;
  27.       id_Error    = 114;
  28.       id_Result   = 115;
  29.       cm_TRootsTest = 101;
  30.  
  31. Type
  32.    TTransferRecord = Record
  33.        Solve       : Array[0..40] of Char;
  34.       X1          : Array[0..7] of Char;
  35.       X2          : Array[0..7] of Char;
  36.       Tolerance   : Array[0..8] of Char;
  37.       IterMax     : Array[0..10] of Char;
  38.       Iter        : Array[0..10] of Char;
  39.       RootValue   : Array[0..40] of Char;
  40.       Error       : Array[0..1] of Char;
  41.       Result      : Array[0..40] of Char;
  42.       Brent       : Bool;
  43.       Bisection   : Bool;
  44.       Newton      : Bool;
  45.    End; {Record}
  46.  
  47. Var
  48.    ABuffer  : TTransferRecord;
  49.  
  50. Type
  51.       PTestWindow = ^TTestWindow; {++++++++++++++++++++++++++++++++++++++++++++++}
  52.       TTestWindow = object(TWindow)
  53.           ADialog  : PDialog;
  54.       ARadioButton : PRadioButton;
  55.       AEdit    : PEdit;
  56.  
  57.         Constructor Init(AParent: PWindowsObject; ATitle: PChar);
  58.         Procedure   TRootsTest(var Msg: TMessage); virtual cm_First + cm_TRootsTest;
  59.       end; {Object}
  60.  
  61.    PTestDialog = ^TTestDialog;
  62.    TTestDialog = Object(TDialog)
  63.        Procedure   xx(Var Msg:TMessage); Virtual id_First + id_Run;
  64.    End; {Object}
  65.  
  66.    PMyRoots = ^TMyRoots; {++++++++++++++++++++++++++++++++++++++++++++++++++++}
  67.    TMyRoots = Object(TRoots)
  68.           Function    fx(X:TFloatingPoint) : TFloatingPoint; Virtual;
  69.       Function    fxPrime(X:TFloatingPoint) : TFloatingPoint; Virtual;
  70.    End; {Object}
  71.  
  72. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  73. {+++ TMyRoots +++++++++++++++++++++++++++++++++++++++++++++++++++ TMyRoots +++}
  74. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  75.  
  76. Function TMyRoots.fx;
  77. {*****************************************************************************}
  78.     Begin
  79.          fx := (2 * x + 3) * (x - 3);
  80.    End;
  81. {EndIf}
  82.  
  83. Function TMyRoots.fxPrime;
  84. {*****************************************************************************}
  85.     Begin
  86.         fxPrime := 4 * x - 3;
  87.    End;
  88. {EndIf}
  89.  
  90.  
  91.  
  92. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  93. {+++ TTestWindow +++++++++++++++++++++++++++++++++++++++++++++ TTestWindow +++}
  94. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  95.  
  96. Constructor TTestWindow.Init(AParent: PWindowsObject; ATitle: PChar);
  97. {*****************************************************************************}
  98.     Begin
  99.           TWindow.Init(AParent, ATitle);
  100.           Attr.Menu := LoadMenu(Hinstance, MakeIntResource(TheMenu));
  101.     End;
  102. {EndConstructor}
  103.  
  104. procedure TTestWindow.TRootsTest(var Msg: TMessage);
  105. {*****************************************************************************}
  106.     Begin
  107.       ADialog := New(PTestDialog, Init(@Self, 'TROOTS'));
  108.       ADialog^.TransferBuffer := @ABuffer;
  109.  
  110.        StrCopy(ABuffer.Solve    , 'f(x) = dunno');
  111.       StrCopy(ABuffer.X1       , '0.1');
  112.       StrCopy(ABuffer.X2       , '4.0');
  113.       StrCopy(ABuffer.Tolerance, '1.0e-10');
  114.       StrCopy(ABuffer.IterMax  , '100');
  115.       StrCopy(ABuffer.Iter     , '');
  116.       StrCopy(ABuffer.RootValue, '');
  117.       StrCopy(ABuffer.Error    , '0');
  118.       StrCopy(ABuffer.Result   , '');
  119.       ABuffer.Brent           := True;
  120.       ABuffer.Bisection       := False;
  121.       ABuffer.Newton          := False;
  122.  
  123.        New(AEdit,        InitResource(ADialog, id_Solve,     SizeOf(ABuffer.Solve)));
  124.       New(AEdit,        InitResource(ADialog, id_X1,        SizeOf(ABuffer.X1)));
  125.       New(AEdit,        InitResource(ADialog, id_X2,        SizeOf(ABuffer.X2)));
  126.       New(AEdit,        InitResource(ADialog, id_Tolerance, SizeOf(ABuffer.Tolerance)));
  127.       New(AEdit,        InitResource(ADialog, id_IterMax,   SizeOf(ABuffer.IterMax)));
  128.        New(AEdit,        InitResource(ADialog, id_Iter,      SizeOf(ABuffer.Iter)));
  129.       New(AEdit,        InitResource(ADialog, id_RootValue, SizeOf(ABuffer.RootValue)));
  130.       New(AEdit,        InitResource(ADialog, id_Error,     SizeOf(ABuffer.Error)));
  131.       New(AEdit,        InitResource(ADialog, id_Result,    SizeOf(ABuffer.Result)));
  132.       New(ARadioButton, InitResource(ADialog, id_Brent));
  133.       New(ARadioButton, InitResource(ADialog, id_Bisection));
  134.       New(ARadioButton, InitResource(ADialog, id_Newton));
  135.  
  136.       Application^.ExecDialog(ADialog);
  137.     End;
  138. {EndProcedure}
  139.  
  140. Procedure TTestDialog.xx;
  141. {*****************************************************************************}
  142.     Var
  143.       ARoot      : PMyRoots;
  144.       Code       : Integer;           { Holds the Val (conversion) error code }
  145.       dX1        : Real;
  146.       dX2        : Real;
  147.       dTolerance : Real;
  148.       dIterMax   : Integer;
  149.  
  150.     Begin
  151.       {-----------------------------------------------------------------------}
  152.       { Data from dialog into transfer record.                                }
  153.       { Data from transfer record to local vars                               }
  154.       {-----------------------------------------------------------------------}
  155.         TransferData(tf_GetData);
  156.         Val(ABuffer.X1, dX1, Code);
  157.       Val(ABuffer.X2, dX2, Code);
  158.       Val(ABuffer.Tolerance, dTolerance, Code);
  159.       Val(ABuffer.IterMax, dIterMax, Code);
  160.  
  161.       ARoot := New(PMyRoots, Init(dX1, dX2, dTolerance, dIterMax));
  162.  
  163.          If (ABuffer.Brent) Then
  164.               Str(ARoot^.BrentRoots:5:2, ABuffer.Result)
  165.       Else If (ABuffer.Bisection) Then
  166.               Str(ARoot^.BisectionRoots:5:2, ABuffer.Result)
  167.       Else    If (ABuffer.Newton) Then
  168.               Str(ARoot^.NewtonRoots:5:2, ABuffer.Result);
  169.       {EndIfCase}
  170.  
  171.       {-----------------------------------------------------------------------}
  172.       { Convert fields to strings                                             }
  173.       { Data from transfer record to dialog                                   }
  174.       {-----------------------------------------------------------------------}
  175.       Str(ARoot^.X1,              ABuffer.X1);
  176.       Str(ARoot^.X2,              ABuffer.X2);
  177.       Str(ARoot^.IterMax,         ABuffer.IterMax);
  178.       Str(ARoot^.Iter,            ABuffer.Iter);
  179.       Str(ARoot^.RootValue:5:2,   ABuffer.RootValue);
  180.       Str(ARoot^.ErrorCode,       ABuffer.Error);
  181.       TransferData(tf_SetData);
  182.  
  183.       Dispose(ARoot, Done);
  184.     End;
  185. {EndProcedure}
  186.  
  187.  
  188.  
  189. {/////////////////////////////////////////////////////////////////////////////}
  190. {\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\}
  191. {/////////////////////////////////////////////////////////////////////////////}
  192.  
  193. Type
  194.    TDlgApplication = object(TApplication)
  195.       Procedure InitMainWindow; virtual;
  196.    End; {Object}
  197.  
  198. Var
  199.    MyApp: TDlgApplication;
  200.  
  201. Procedure TDlgApplication.InitMainWindow;
  202. {*****************************************************************************}
  203.     Begin
  204.           MainWindow := New(PTestWindow, Init(nil, 'Natural Systems'));
  205.     End;
  206. {EndProcedure}
  207.  
  208. Begin {=======================================================================}
  209.    MyApp.Init('DialTest');
  210.    MyApp.Run;
  211.    MyApp.Done;
  212. End.